package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        // 封装当前用户的id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 判断当前商品是首次添加 还是 已经添加过
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByMany(shoppingCart);
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            // 说明当前商品已经存在购物车  数量加一
            // 获取数据库查出来的本条商品数据
            ShoppingCart dbShoppingCart = shoppingCarts.get(0);
            // 将数量加一
            dbShoppingCart.setNumber(dbShoppingCart.getNumber() + 1);
            // 将增加份数的数据更新至表中
            shoppingCartMapper.updateShoppingCart(dbShoppingCart);
        } else {
            // 说明当前商品不在购物车 首次添加
            // 先判断当前加购的商品是菜品或者套餐
            if (shoppingCart.getDishId() != null) {
                // 说明当前是菜品加购
                // 封装菜品的数据 以及 口味
                Dish dish = dishMapper.selectDishById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                // 添加到购物车表中
                shoppingCartMapper.insertShoppingCart(shoppingCart);
            } else {
                // 说明当前是套餐加购
                Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                // 添加到购物车表中
                shoppingCartMapper.insertShoppingCart(shoppingCart);
            }
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> getShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();

        return shoppingCartMapper.selectByMany(shoppingCart);
    }

    /**
     * 减少购物车商品数量
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 根据现有的用户id 套餐或菜品id 到购物车表中查出本条数据的所有信息
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByMany(shoppingCart);
        // 获取当前操作的数据
        ShoppingCart dbShoppingCart = shoppingCarts.get(0);
        // 判断当前商品的数量
        if (dbShoppingCart.getNumber() == 1) {
            // 等于1  直接删除本条数据
            shoppingCartMapper.deleteShoppingCart(dbShoppingCart);
        } else {
            // 大于1  数量减一
            // 更新数量 减一
            dbShoppingCart.setNumber(dbShoppingCart.getNumber() - 1);
            // 更新数据
            shoppingCartMapper.updateShoppingCart(dbShoppingCart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        shoppingCartMapper.deleteShoppingCart(shoppingCart);
    }
}
